package edu.sctu.practice.class02.hujie.online_classes_practice.imooc.Java_XMLRead.SAX;

import com.google.common.io.FileBackedOutputStream;
import com.sun.xml.internal.stream.events.AttributeImpl;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * Created by hp on 2016/3/8.
 */
public class SAXtest {
    public ArrayList<Book> parseXML() {
        //获取一个SAXParserFactory的实例
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //通过factory获取SAXParser实例
        SAXParserHandler handler=null;
        try {
            SAXParser parser = factory.newSAXParser();
            //创建SAXParser对象
            handler = new SAXParserHandler();
            parser.parse("src\\main\\java\\edu\\sctu\\practice\\class02\\hujie\\online_classes_practice\\imooc\\Java_XMLRead\\books.xml", handler);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return handler.getBookList();
    }

    public void createXML(){
        ArrayList<Book> bookList=parseXML();
        //生成xml
        //1.创建一个TransformerFactory类的对象
        SAXTransformerFactory tff=(SAXTransformerFactory)SAXTransformerFactory.newInstance();
        try {
            //2.通过SAXTransformerFactory对象创建一个TransformerHandler对象
            TransformerHandler handler=tff.newTransformerHandler();
            //3.通过handler对象创建一个Transformer对象
            Transformer tr=handler.getTransformer();
            //4.通过Transformer对象对生成的xml文件进行设置
            //设置xml编码
            tr.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
            //设置xml是否换行
            tr.setOutputProperty(OutputKeys.INDENT,"yes");//换行
            //5.创建一个Result对象
            File f=new File("src\\main\\java\\edu\\sctu\\practice\\class02\\hujie\\online_classes_practice\\imooc\\Java_XMLRead\\books2.xml");
            if (!f.exists()){
                f.createNewFile();
            }
            //6.创建Result对象，并且使其与handler关联
            Result result=new StreamResult(new FileOutputStream(f));
            handler.setResult(result);
            //7.利用handler对象进行xml文件内容的编写
            //打开document
            handler.startDocument();
            AttributesImpl attr=new AttributesImpl();
            handler.startElement("", "", "bookstore", attr);
            for (Book book:bookList){
                attr.clear();
                attr.addAttribute("", "", "id", "", book.getId());
                handler.startElement("", "", "book", attr);
                //创建name节点
                if (book.getName()!=null&&book.getName().trim().equals("")){
                    attr.clear();
                    handler.startElement("", "", "name", attr);
                    handler.characters(book.getName().toCharArray(), 0, book.getName().length());
                    handler.endElement("", "", "name");
                }
                //创建year节点
                if (book.getYear()!=null&&book.getYear().trim().equals("")) {
                    attr.clear();
                    handler.startElement("", "", "year", attr);
                    handler.characters(book.getYear().toCharArray(), 0, book.getYear().length());
                    handler.endElement("", "", "year");
                }
                if (book.getAuthor()!=null&&book.getAuthor().trim().equals("")) {
                    //创建author节点
                    attr.clear();
                    handler.startElement("", "", "author", attr);
                    handler.characters(book.getAuthor().toCharArray(), 0, book.getAuthor().length());
                    handler.endElement("", "", "author");
                }
                //创建price节点
                if (book.getPrice()!=null&&book.getPrice().trim().equals("")) {
                    attr.clear();
                    handler.startElement("", "", "price", attr);
                    handler.characters(book.getPrice().toCharArray(), 0, book.getPrice().length());
                    handler.endElement("", "", "price");
                }
                //创建language节点
                if (book.getLanguage()!=null&&book.getLanguage().trim().equals("")) {
                    attr.clear();
                    handler.startElement("", "", "language", attr);
                    handler.characters(book.getLanguage().toCharArray(), 0, book.getLanguage().length());
                    handler.endElement("", "", "language");
                    handler.endElement("","","book");

                }
            }
            handler.endElement("","","bookstore");
            //结束document
            handler.endDocument();
       } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }catch (NullPointerException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SAXtest test= new SAXtest();
        test.createXML();
    }
}
